﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using info.lundin.math;
using System.Collections;

namespace ChMetody_3
{
    class Functions
    {
        public static double f(string s, double x)
        {
            ExpressionParser Parser = new ExpressionParser();
            Hashtable h = new Hashtable();
            h.Add("x", x.ToString());
            return Parser.Parse(s, h);
        }

        public static List<double> Ribakoff(double a, double b, string s, double eps)
        {
            double x;
            double M = analyseM(s, a, b);
            List<double> Korni = new List<double>();
            x = a;
            while (x < b){
                if (Math.Abs(f(s, x)) <= eps)
                {
                    Korni.Add(x);
                    x += 2*eps;
                }
                else
                {
                    x = x + Math.Abs(f(s, x) / M);
                }
            }
            return Korni;
        }

        private static double analyseM(string s, double a, double b)
        {
            double x = a;
            double h = (b-a)/1000;
            double y;
            double M = 0;
            do
            {
                y = Math.Abs(diff(s, x));
                if (M < y)
                {
                    M = y;
                }
                x += h;
            } while (x > b);
            return 2 * M;
        }

        private static double diff(string s, double x)
        {
            double h = 0.000001;
            return (f(s, x + h) - f(s, x-h)) / (2 * h);
        }
    }
}
